<?php
namespace Api\Controller;
use Think\Controller;
use Think\Vender;
class LogoController extends Controller {
    public function index()
    {
    	echo "logo";
    }
   //保存登录信息
    public function getuserinfo(){
    	if(!isset($_POST["code"]) || empty($_POST["code"])){
			 $data = array("code"=>400,"msg" =>"获取失败：未获取到code！");
             $this->ajaxReturn($data);
    	}
   		$code=$_POST["code"];
   		$userinfo['nickname']=$_POST["nickName"];
   		$userinfo['province']=$_POST["province"];
   		$userinfo['avatarUrl']=$_POST["avatarUrl"];
   		$userinfo['city']=$_POST["city"];
   		$userinfo['sex']=$_POST["gender"];
   		$userinfo['logo_time']=time();


     	$config=D('Config')->get_config();
     	if($config){
     		$data=A('GetUserInfo');
     		//$this->ajaxReturn($data->get_userinfo($config['appid'],$config['appsecret'],$code,$userinfol));
     		$result=$data->get_userinfo($config['appid'],$config['appsecret'],$code);			//获取openid 
     		if($result['error']==200){
     			$userinfo['openid']=$result['openid'];
	     		$re=M('user')->where(array('openid'=>$userinfo['openid']))->find();						    //查询是否是第一次登陆
	     		if($re){																		                                      //判断用户第几次登录修改最后时间与登录次数
	     			$upload['last_time']=time();
	     			$upload['logo_sum']=$re['logo_sum']+1;
	     			$resu=M('user')->where(array('openid'=>$userinfo['openid']))->save($upload);
	     			if($resu){                                                                       //判断用户是否修改信息成功
	     				$data = array("code"=>200,"data"=>$re);                                        //修改成功说明有查询到用户，把$re数据返回回去
	             		$this->ajaxReturn($data);
	     			}else{
	     				$data = array("code"=>400, "msg"=>"更改用户登录次数与时间失败","data"=>$resu);
	             		$this->ajaxReturn($data);
	     			}
	     		}else{
            $userinfo['last_time']=0;
	     			$resul=M('user')->add($userinfo);                                                //add()的返回值就是当前主键的值
            $user_add=M('user')->where(array('id'=>$resul))->find();                         //返回新增加用户的数据
	     			if($resul){
	     				$data = array("code"=>200,"data"=>$user_add);
	     				$this->ajaxReturn($data);
	     			}
	     			else {
	     				$data = array("code"=>400,"msg"=>"添加用户初次登录信息失败");
	     				$this->ajaxReturn($data);
	     			}
	     		}
     		}else{
     			 $data = array("code"=>400,"msg"=>"获取openid失败");
            	 $this->ajaxReturn($data);
     		}
     	}else{
     		 $data = array("code"=>400,"msg"=>"获取小程序配置错误");
             $this->ajaxReturn($data);
     	}
    }

    public function ceshi(){
      echo 123;
    }
}	